package whr.modules.assets.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import whr.common.utils.PageUtils;
import whr.common.utils.Query;

import whr.modules.assets.dao.ReceiveDao;
import whr.modules.assets.dao.RepairsDao;
import whr.modules.assets.entity.InfoEntity;
import whr.modules.assets.entity.ReceiveEntity;
import whr.modules.assets.entity.RepairsEntity;
import whr.modules.assets.service.InfoService;
import whr.modules.assets.service.ReceiveService;
import whr.modules.assets.service.RepairsService;


@Service("repairsService")
public class RepairsServiceImpl extends ServiceImpl<RepairsDao, RepairsEntity> implements RepairsService {
    @Autowired
    private RepairsService repairsService;
    @Autowired
    private RepairsDao repairsDao;
    @Autowired
    private ReceiveService receiveService;
    @Autowired
    private InfoService infoService;
    @Autowired
    private ReceiveDao receiveDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<RepairsEntity> page = this.page(
                new Query<RepairsEntity>().getPage(params),
                new QueryWrapper<RepairsEntity>()
                        .like(StringUtils.isNotBlank((String) params.get("key")),"assets_num",params.get("key"))
                        .eq(StringUtils.isNotBlank((String) params.get("key2"))," repairs_status",params.get("key2"))
                        .orderByAsc(" repairs_status")
        );

        return new PageUtils(page);
    }
    public PageUtils queryByRole(Map<String, Object> params) {
        //根据id查角色
        Long userId = Long.parseLong((String) params.get("userId"));
        List<String> userRoles = receiveDao.selectUserRoles(userId);
        for (String userRole :userRoles  ) {  // 只有人事主管可以审批看到所有信息
            if(userRole.equals("人事主管")){
                PageUtils page = repairsService.queryPage(params);

                return page;
            }
        }
        //是普通员工的话
        IPage<RepairsEntity> page = this.page(
                new Query<RepairsEntity>().getPage(params),
                new QueryWrapper<RepairsEntity>()
                        .eq( "user_name",params.get("userName"))
                        .like(StringUtils.isNotBlank((String) params.get("key")),"assets_num",params.get("key"))
                        .eq(StringUtils.isNotBlank((String) params.get("key2")),"repairs_status",params.get("key2"))
                        .orderByAsc("repairs_status")
        );

        return new PageUtils(page);
    }

    /**
     *  1.把数据存入维修表中
     *  2.改变领用表中的状态改为报修审核中 3 并把报修描述更新
     *
     */
    @Override
    public void applySave(RepairsEntity repairs) {
        repairsService.save(repairs);
        //根据assetsNum=474更新领用表中状态数据
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("assets_num",repairs.getAssetsNum());
        queryWrapper.eq("create_status","1");
        ReceiveEntity one = receiveService.getOne(queryWrapper);
        one.setCreateStatus("3");  // 报修审核中
        one.setRepairsDesc(repairs.getRepairsDesc());
        one.setRepairsTime(repairs.getRepairsTime());
        one.setRepairsPic(repairs.getRepairsPic());
        receiveService.updateById(one);


    }

    /** repairsRepairsEntity(repairsId=35, repairsTime=Thu Sep 26 00:00:00 CST 2024, userName=user,
     * userMobile=null, assetsNum=98, repairsStatus=2, assetsName=椅子, repairsDesc=坏了, repairsPic=null)
     * 根据
     * @param repairs  根据资产编号查
     * 1.把维修状态改为 维修完成 1（维修表）
     * 2.领用表状态改为使用中 1 （领用表）
     * 3.资产表改为使用中 2    （资产表）
     */
    @Override
    public void updateRepairs(RepairsEntity repairs) {
        //把维修状态改为 维修完成 1（维修表）
        String assetsNum =repairs.getAssetsNum();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("assets_num",assetsNum);
        queryWrapper.eq("repairs_status","2");  // 查 资产和维修状态为维修中的
        RepairsEntity one = repairsService.getOne(queryWrapper);
        one.setRepairsStatus("1");
        repairsService.updateById(one);
        //领用表状态改为使用中 1 （领用表）
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("assets_num",assetsNum);
        queryWrapper1.eq("create_status","2");     ///2维修中
        ReceiveEntity one1 = receiveService.getOne(queryWrapper1);
        one1.setCreateStatus("1");
        receiveService.updateById(one1);
        // 资产表改为使用中 2    （资产表）
        QueryWrapper queryWrapper2 = new QueryWrapper();
        queryWrapper2.eq("assets_num",assetsNum);
        InfoEntity one2 = infoService.getOne(queryWrapper2);
        one2.setAssetsStatus("2");
        infoService.updateById(one2);
    }

}